package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.bookstore.Return;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.ReturnService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class ReturnServiceImpl extends HibernateGenericService<Return, Long> implements ReturnService {

	@Override
	public Collection<Return> find(Date date, User user) {
		Criteria criteria = this.createCriteria();
		if(!Checker.isNull(date)) {		
			criteria.add(Restrictions.eq("date", date));
		}
		if(!Checker.isNull(user)) {
			criteria.add(Restrictions.eq("returner", user));
		}
		criteria.addOrder(org.hibernate.criterion.Order.desc("date"));
		return criteria.list();
	}

	@Override
	public List<Return> findByDateAsc(Date fromDate, Date toDate) {
		Criteria criteria = this.createCriteria();
		if(!Checker.isNull(fromDate)) {
			criteria.add(Restrictions.ge("date", fromDate));
		}
		if(!Checker.isNull(toDate)) {
			criteria.add(Restrictions.le("date", toDate));
		}
		criteria.addOrder(org.hibernate.criterion.Order.asc("id"));
		return criteria.list();
	}
}